<template>
	<div>
		  <div class="gva-table-box">
		    <div class="gva-btn-list"><el-text class="mx-1" size="large">今日触发次数</el-text></div>
		    <el-card>
		      <el-row style="margin:30px auto">
		        <el-col :span="2" />
		        <el-col :span="5">
		          <el-statistic
		            title="广告曝光"
		           
		          />
		          <el-text class="mx-1" size="small"></el-text>
		        </el-col>
		    
		        <el-col :span="5">
		          <el-statistic
		            title="完整播放量"
		           
		          />
		          <el-text class="mx-1" size="small"></el-text>
		        </el-col>
		        <el-col :span="5">
		          <el-statistic
		            title="曝光PV"
		            
		          />
		          <el-text class="mx-1" size="small">官方数据</el-text>
		        </el-col>
		     
		        <el-col :span="5">
		          <el-statistic
		            title="完整播放率"
		            
		          >
		          <template #suffix>%</template>
		            </el-statistic>
		            <el-text class="mx-1" size="small"></el-text>
		        </el-col>
		        <el-col :span="2" />
		      </el-row>
		    </el-card>
			
			
		  </div>
			 <div class="gva-table-box">
				 <div style="margin-bottom: 10px;">
				 <el-button type="primary" @click="taggerForm">新建规则</el-button>
				   </div>				   
				 <el-table
				   ref="multipleTable"
				   tooltip-effect="dark"
				   row-key="ID"
				   border
				   :data = "rulesList"
				   highlight-current-row
				 >
				 	<el-table-column align="center" type="index" label="序号" width="80" />
				   <el-table-column
				     align="left"
				     label="规则名称"
				     width="200px"
				     prop="name"
				   />
				   <el-table-column
				     align="left"
				     label="应用对象"
				     width="120px"
				     prop="objectTo"
				   />
				   <el-table-column
				     align="left"
				     label="相关条件"
				     prop="conditionInfo"
				   />
				   <el-table-column
				     align="left"
				     label="相关条件"
				     prop="executeInfo"
				   />
				   <el-table-column
				     align="center"
				     label="开/关"
					 width="80px"
					>
					<template #default="scope">
						<el-switch
							v-model="scope.row.status"
							size="small"
							@change="statusChange(scope.row)"
						/>
						</template>
				   </el-table-column>
				   <el-table-column
				     align="center"
				     label="操作"
				     width="200px"
				   	>
				     <template #default="scope">
						<el-popover
							v-model:visible="scope.row.visible"
							placement="top"
							width="160"
							>
							<p>确定要删除此规则吗</p>
							<div style="text-align: right; margin-top: 8px;">
								<el-button
								type="primary"
								link
								@click="scope.row.visible = false"
								>取消</el-button>
								<el-button
								type="primary"
								@click="del(scope.row);"
								>确定</el-button>
							</div>
							<template #reference>
								<el-button
								type="primary"
								link
								icon="delete"
								>删除</el-button>
							</template>
							</el-popover>
							<el-button
							type="primary"
							link
							icon="edit"
							@click="openEdit(scope.row)"
							>编辑</el-button>
				     </template>
				   </el-table-column>
				 </el-table>
				 <div class="gva-pagination">
					<el-pagination
					:current-page="page"
					:page-size="pageSize"
					:page-sizes="[10, 30, 50, 100]"
					:total="total"
					layout="total, sizes, prev, pager, next, jumper"
					@current-change="handleCurrentChange"
					@size-change="handleSizeChange"
					/>
				</div>
			 </div>
			 
		
		   <el-drawer v-model="drawerForm" size="60%">
			 <template #header>
			   <h4>创建规则</h4>
			 </template>
			 <template #default>
				<div style="overflow:auto;padding:0 12px;">
				  <el-form
				    ref="ruleFormRef"
				    :model="ruleForm"
				    label-width="80px"
					label-position="top"
					:rules="rules"
				  >
				  
					<el-form-item label="应用对象" prop="objectTo">
					  <el-radio-group v-model="ruleForm.objectTo" @change="objectToChange">
					      <el-radio-button v-for="(item,index) in objects" :value="index" :label="item" />
					    </el-radio-group>
					</el-form-item>
					<el-form-item label="选择账号" v-if="ruleForm.objectTo=='account'" prop="accounts">
						<el-radio-group v-model="ruleForm.accountSelectStatus" style="width: 100%;">
					      <el-radio-button v-for="item in accountSelectStatus" :value="item.value" :label="item.label" />
					    </el-radio-group>

						<el-cascader-panel v-if="ruleForm.accountSelectStatus=='1'"
							:options="accounts"
							:props="accountProps"
							collapse-tags
							collapse-tags-tooltip
							clearable
							:show-all-levels="false"
							style="width: 70vh;margin-top: 1vh"
							v-model = "ruleForm.accounts"
						/>
					</el-form-item>
					<el-form-item label="选择项目" v-if="ruleForm.objectTo=='project'" prop="projects">
						<el-radio-group v-model="ruleForm.projectSelectStatus" style="width: 100%;">
					      <el-radio-button v-for="item in projectSelectStatus" :value="item.value" :label="item.label" />
					    </el-radio-group>

						<el-cascader-panel v-if="ruleForm.projectSelectStatus=='1'"
							:options="projects"
							:props="accountProps"
							collapse-tags
							collapse-tags-tooltip
							clearable
							:show-all-levels="false"
							style="width: 40vh;margin-top: 1vh"
							v-model = "ruleForm.projects"
						/>
					</el-form-item>
					<el-form-item label="选择广告" v-if="ruleForm.objectTo=='adv'" prop="advs">
						<el-radio-group v-model="ruleForm.advSelectStatus" style="width: 100%;">
					      <el-radio-button v-for="item in advSelectStatus" :value="item.value" :label="item.label" />
					    </el-radio-group>
						<el-cascader-panel v-if="ruleForm.advSelectStatus=='1'"
							:options="advs"
							:props="accountProps"
							collapse-tags
							collapse-tags-tooltip
							clearable
							:show-all-levels="false"
							style="width: 70vh;margin-top: 1vh"
							v-model = "ruleForm.advs"
						/>
					</el-form-item>
					<el-form-item label="触发条件" prop="condition">
					
					<div style="margin-top: 1vh;" v-for="(item,index) in ruleForm.condition" :key="index">
						<el-select
							placeholder="选择"
							style="width: 120px;margin-right: 1vh;"
							v-model="item.time"
							>
							<el-option
								v-for="(item,index) in conditionTime"
								:label="item"
								:value="index"
							/>
						</el-select>
						<el-cascader style="margin-right: 1vh;" v-model="item.target" :options="conditionTarget" @change="targetChange($event,index)" />
						<el-select
							placeholder="选择"
							style="width: 120px;margin-right: 1vh;"
							v-model="item.op"
							>
							<el-option
								v-for="iitem in item.opItem"
								:label="conditionOp[iitem]"
								:value="iitem"
							/>
						</el-select> 
						<template v-if="item.op=='belong'">
								<el-select
									placeholder="选择"
									style="width: 120px"
									v-model="item.value1"
									v-if="item.target == 'adStatus'"
									>
									<el-option
										v-for="(iitem,index) in conditionTargetAdStatus"
										:label="iitem.label"
										:value="iitem.value"
									/>
								</el-select>
								<el-select
									placeholder="选择"
									style="width: 120px"
									v-model="item.value1"
									v-if="item.target == 'studyStatus'"
									>
									<el-option
										v-for="(iitem,index) in conditionTargetStudyStatus"
										:label="iitem.label"
										:value="iitem.value"
									/>
								</el-select>
						</template>
						<template v-else>
							<el-input type="number" placeholder="请输入" v-model="item.value1" style="width: 100px"/> <span v-if="item.op=='between'">- <el-input type="number" placeholder="请输入" v-model="item.value2" style="width: 100px"/></span> 
						</template>
						<el-tooltip v-if="ruleForm.condition.length<=1 ? true : false" content="规则至少需要设置一个条件" placement="top" effect="light">
							<el-button disabled icon="DeleteFilled" style="margin-left: 1vh;"/> 
						</el-tooltip>
						<el-button v-if="ruleForm.condition.length>1 ? true : false"  icon="DeleteFilled" @click="deleteCondition(index)"  style="margin-left: 1vh;"/> 
					</div>
					<el-button style="width: 100%;margin-top: 10px;border: 1px dashed var(--el-border-color);" @click="addCondition">+新增条件</el-button>
					</el-form-item>
					<h5 style="color: #b1b3b8;" type="info" size="small"> <el-icon><InfoFilled /></el-icon> 以上条件同时满足，才会触发本条规则</h5>

					<el-form-item label="要执行的操作" prop="">
						<el-cascader v-model="ruleForm.execute.opt" :options="executes[ruleForm.objectTo]" style="margin-right: 1vh;"/>
						<template v-if="ruleForm.execute.opt[0]!='status' && ruleForm.execute.opt[0]!='notice'">
							<el-input type="number" placeholder="请输入" v-model="ruleForm.execute.value1" style="width: 100px;margin-right: 1vh;"/> 
						
							<el-select
								placeholder="选择"
								style="width: 100px;margin-right: 1vh;"
								v-model="ruleForm.execute.value1Unit"
								>
								<el-option
									v-for="item in units"
									:label="item.label"
									:value="item.value"
								/>
							</el-select>
							<template v-if="ruleForm.execute.opt[1]!='setTo'">						
								<el-text v-if = "ruleForm.execute.opt[1] =='decrease'"  style="margin-right: 1vh;">最低调整至 </el-text> 
								<el-text v-if = "ruleForm.execute.opt[1] =='increase'"  style="margin-right: 1vh;">最高调整至 </el-text> 
								<el-input  type="number" placeholder="请输入" v-model="ruleForm.execute.value2" style="width: 120px;margin-right: 1vh;">
									<template #append>元</template>
									</el-input>
							</template>
						</template>
						<template v-if="getExeciteFrequency">
							<el-text style="margin-right: 1vh;">调整频率</el-text><el-select
								placeholder="选择"
								style="width: 160px;margin-right: 1vh;"
								v-model="ruleForm.execute.frequency"
								>
								<el-option v-for="item in getExeciteFrequency"
									:label="item.label"
									:value="item.value"
									/>
							</el-select> 
							<el-select
								placeholder="选择"
								style="width: 80px"
								v-model="ruleForm.execute.frequencyValue"
								>
								<el-option v-for="item in frequencys"
									:label="item.label"
									:value="item.value"
									/>
							</el-select>
						</template>
						
					</el-form-item>
					<h5 style="color: #b1b3b8;" type="info" size="small"> <el-icon><InfoFilled /></el-icon> 盯盘规则内容包含“预算”的盯盘规则，对于不限预算的项目不会生效。</h5>
					<el-form-item label="执行时间" prop="">
						<el-radio-group v-model="ruleForm.executeTime.time">
					      <el-radio-button v-for="item in executeTimes" :value="item.value" :label="item.label" />
					    </el-radio-group>	
						
					</el-form-item>
					<el-form-item label="" prop="" v-if="ruleForm.executeTime.time == 'day'">
						<el-time-select
							v-model="ruleForm.executeTime.value"
							style="width: 240px"
							start="00:00"
							step="01:00"
							end="23:00"
							placeholder="选择时间"
						/>
					</el-form-item>
					<el-form-item label="规则名称" prop="name">
				      <el-input v-model="ruleForm.name"  style="width: 50%;" maxlength="50" placeholder="请输入规则名称" show-word-limit/>
				    </el-form-item>
				  </el-form>
			   </div>
			 </template>
			 <template #footer>
			   <div style="flex: auto;">
				 <el-button @click="taggerForm">取消</el-button>
				 <el-button type="primary" @click="submitForm()">创建</el-button>
			   </div>
			 </template>
		   </el-drawer>
	</div>
</template>
<script lang="ts" setup>
	import { reactive,ref,onMounted, computed } from 'vue'
	import { rpcGet } from '@/api/rpc'; '@api/rpc'
	import { ElMessage, ElMessageBox } from 'element-plus'
	const drawerForm = ref(false)
	const taggerForm = () => {
		ruleForm.value = JSON.parse(JSON.stringify(defaultForm))
		drawerForm.value = !drawerForm.value
	}
	const objects = {
		"account":"应用至账户",
		"project":"应用至项目",
		"adv":"应用至广告",
		//"material":"应用至素材"
	}
	const accountSelectStatus = [
		{
			value: '0',
			label: '全部账号',
		},
		{
			value: '1',
			label: '选择账号',
		},
	]
	const projectSelectStatus = [
		{
			value: '0',
			label: '全部项目',
		},
		{
			value: '1',
			label: '选择项目',
		},
	]
	const advSelectStatus = [
		{
			value: '0',
			label: '全部广告',
		},
		{
			value: '1',
			label: '选择广告',
		},
	]
	const conditionTime = {
		today : "当日",
		threeDay : "过去3天",
		fiveDay : "过去5天",
		sevenDay : "过去7天",
	}
	const conditionOp = {
		belong: "属于",
		geq:"大于等于",
		leq:"小于等于",
		between:"介于"
	}
	const conditionTargetAdv  = [
		{
			value: 'adStatus',
			label: '广告状态',
			opItem:["belong"]
		},
		{
			value: 'studyStatus',
			label: '学习期状态',
			opItem:["belong"]
		},
	]
	const conditionTargetAdStatus = [
		{
			value: 'PROMOTION_STATUS_ENABLE',
			label: '投放中',
		},
		{
			value: 'PROMOTION_STATUS_DISABLE-DISABLED',
			label: '未投放-已暂停',
		},
		{
			value: 'PROMOTION_STATUS_DONE',
			label: '未投放-已完成',
		},
		{
			value: '',
			label: '未投放-广告超出预算',
		},
		]
	const conditionTargetStudyStatus = [
		{
			value: 'LEARNING',
			label: '学习期中',
		},
		{
			value: 'LEARNED',
			label: '学习成功',
		},
		{
			value: 'LEARN_FAILED',
			label: '学习失败',
		},
	]
	const conditionTargetCommon = [
		{
			value: 'adStatus',
			label: '广告状态',
			opItem:["belong"]
		},
		{
			value: 'studyStatus',
			label: '学习期状态',
			opItem:["belong"]
		},
		{
			value: 'showData',
			label: '展示数据',
			opItem:["geq","leq","between"],
			children:[
				{
					value: 'cost',
					label: '消耗',
				},
				{
					value: 'click',
					label: '点击次数',
				},
				{
					value: 'avg_click_cost',
					label: '平均点击单价',
				},
				{
					value: 'ctr',
					label: '点击率',
				},
				{
					value: 'show',
					label: '展示数',
				},
				{
					value: 'balance',
					label: '账户余额',
				},
			]
		},
		{
			value: 'transformData',
			label: '转化数据',
			opItem:["geq","leq","between"],
			children:[
				{
					value: 'convert',
					label: '转化数',
				},
				{
					value: 'convert_rate',
					label: '转化率',
				},
				{
					value: 'convert_cost',
					label: '平均转化成本',
				},
			]
		},
	]
	const executes = {
		"adv" : [
			{
				label:"开关广告",
				value:"status",
				children:[
					{
						label:"开启",
						value:"open",
					},
					{
						label:"关闭",
						value:"close",
					}
				]
			},
			{
				label:"更新广告预算",
				value:"budget",
				children:[
					{
						label:"提高",
						value:"increase",
						
					},
					{
						label:"降低",
						value:"decrease",
						
					},
					{
						label:"调整至",
						value:"setTo",
					}
				]
			},
			{
				label:"更新广告出价",
				value:"offer",
				children:[
					{
						label:"提高",
						value:"increase",
						
					},
					{
						label:"降低",
						value:"decrease",
						
					},
					{
						label:"调整至",
						value:"setTo",
					}
				]
			},
			{
				label:"仅发送通知",
				value:"notice",
				defineChildren:[
					{
						label:"每天每个广告",
						value:"frequency",
					}
				]
			}
		],
		"project":[
			{
				label:"更新项目预算",
				value:"budget",
				children:[
					{
						label:"提高",
						value:"increase",
						
					},
					{
						label:"降低",
						value:"decrease",
						
					},
					{
						label:"调整至",
						value:"setTo",
					}
				]
			},
			{
				label:"仅发送通知",
				value:"notice",
				defineChildren:[
					{
						label:"每天每个项目",
						value:"frequency",
					}
				]
			}
		],
		"account":[
		{
				label:"更新账户预算",
				value:"budget",
				children:[
					{
						label:"提高",
						value:"increase",
						
					},
					{
						label:"降低",
						value:"decrease",
						
					},
					{
						label:"调整至",
						value:"setTo",
					}
				]
			},
			{
				label:"仅发送通知",
				value:"notice",
				defineChildren:[
					{
						label:"每天",
						value:"frequency",
					}
				]
			}
		]
	}
	const units = [
		{
			label:"元",
			value:"yuan",
		},
		{
			label:"%",
			value:"%",
		}
	]
	const frequencys = []
	for(let i=1;i<=5;i++){
		frequencys.push({label:i+"次",value:i})
	}
	frequencys.push({label:"不限",value:0})
	const executeTimes = [
		{
			label:"每天",
			value:"day",
		},
		{
			label:"每小时",
			value:"house",
		},
		{
			label:"每半小时",
			value:"halfHouse",
		},
		{
			label:"每15分钟",
			value:"quarterHouse",
		}
	]
	const conditionInfo = {
		time:'today',
		target:['showData','cost'],
		op:"geq",
		opItem:["geq","leq","between"],
		value1:'',
		value2:''
	}

	const validator = (rule: any, value: any, callback: any)=>{
		
		switch(rule.field){
			case "accounts":
				if(ruleForm.value.objectTo=="account" && ruleForm.value.accountSelectStatus!="0" && ruleForm.value.accounts.length<=0){
					callback(new Error('至少选择一个账号'))		
				}
				break;
			case "projects":
				if(ruleForm.value.objectTo=="project" && ruleForm.value.projectSelectStatus!="0" && ruleForm.value.projects.length<=0){
					callback(new Error('至少选择一个项目'))		
				}
				break;
			case "advs":
				if(ruleForm.value.objectTo=="adv" && ruleForm.value.advSelectStatus!="0" && ruleForm.value.advs.length<=0){
					callback(new Error('至少选择一个广告'))		
				}
				break;
			case "condition":
				ruleForm.value.condition.forEach(item=>{
					if(item.value1==""){
						callback(new Error('请输入数值'))	
					}
					if(item.op=="between" && item.value2==""){
						callback(new Error('请输入完整数值'))	
					}
				})
				break;		
		}
		callback()
	}

	const rules = ref({
		accounts:[{validator:validator, trigger: 'blur' }],
		projects:[{validator:validator, trigger: 'blur' }],
		advs:[{validator:validator, trigger: 'blur' }],
		condition:[{validator:validator, trigger: 'blur' }],
		name: [
			{ required: true, message: '输入规则名称', trigger: 'blur' },
			{ min: 1, max: 50, message: '最长不超过50个字', trigger: 'blur' },
		],
	})
	const defaultForm = {
		id:"",
		name : "",
		objectTo : "account",
		accounts:[],
		projects:[],
		advs:[],
		accountSelectStatus:"0",
		projectSelectStatus:"0",
		advSelectStatus:"0",
		condition:[JSON.parse(JSON.stringify(conditionInfo))],
		execute:{opt:["notice"],value1:"",value2:"",value1Unit:"yuan",frequency:"frequency",frequencyValue:1},
		executeTime:{time:"day",value:"00:00"}
	}
	const ruleForm = ref(JSON.parse(JSON.stringify(defaultForm)))
	const conditionTarget = ref([...conditionTargetCommon])
	const addCondition = () => {
		ruleForm.value.condition.push(JSON.parse(JSON.stringify(conditionInfo)))
	}
	
	const objectToChange = (e: string) => {
		resetAndremoveTargetAdv(e)
		if(ruleForm.value.objectTo=="project")
			getProjects()
		if(ruleForm.value.objectTo=="adv"){
			getAdvs()
			console.log(advs.value)
		}
		ruleForm.value.condition = [JSON.parse(JSON.stringify(conditionInfo))]
		ruleForm.value.execute = defaultForm.execute
		
	}
	const resetAndremoveTargetAdv = (e)=>{
		conditionTarget.value =  [...conditionTargetCommon]
		if(e != "adv"){
			//移除广告特有选项
			conditionTargetAdv.forEach(item=>{
				conditionTarget.value =  conditionTarget.value.filter(target => target.value !== item.value); 
			})
		}
	}
	resetAndremoveTargetAdv("account")
	const targetChange = (e,index)=>{
		const found = conditionTarget.value.find(obj => obj.value === e[0]);
		ruleForm.value.condition[index].opItem = found.opItem
		ruleForm.value.condition[index].op = found.opItem[0]
		ruleForm.value.condition[index].value1 = ""
	}
	const deleteCondition = (index) =>{
		ruleForm.value.condition.splice(index,1)
	}
	const getExeciteFrequency = computed(()=>{
		let obj = executes[ruleForm.value.objectTo].filter(exec => exec.value === ruleForm.value.execute.opt[0])[0]
		if(ruleForm.value.execute.opt.length<=1)
			return obj.defineChildren
		else
			return obj.children.filter(exec => exec.value === ruleForm.value.execute.opt[1])[0].defineChildren
	})
	const ruleFormRef = ref(null)
	const submitForm = () => {
		ruleFormRef.value.validate(async valid => {
			if (valid) {
				let form = JSON.parse(JSON.stringify(ruleForm.value))
				form.accounts = JSON.stringify(ruleForm.value.accounts)
				form.projects = JSON.stringify(ruleForm.value.projects)
				form.advs = JSON.stringify(ruleForm.value.advs)

				form.condition = JSON.stringify(ruleForm.value.condition)
				form.execute = JSON.stringify(ruleForm.value.execute)
				form.executeTime = JSON.stringify(ruleForm.value.executeTime)
				rpcGet({server:"HugeServiceServer.ruleSave",...form}).then(r=>{
					if(r.code == 0){
						ElMessage({
							type: 'success',
							message: '保持成功',
						})
						getRules()
						taggerForm()
					}
				})
			}
		})
		}
	let id = 0
	const accountProps = { 
		multiple: true ,
	}
	const accounts = ref([])
	const getAccounts = ()=>{
		accounts.value = []
		let platforms = [1]
		Promise.all(platforms.map(platform => rpcGet({platformId:platform,server:"HugeServiceServer.GetAccounts"}))).then(results => {  
			results.forEach(r=>{
				if(r.data.total>0){
					r.data.list.forEach(item=>{
						let value = item.plat_form+","+item.advertiser_id
						let itemObj = {
							label:item.advertiser_name,
							value:value
						}
						let objs = accounts.value.filter(acc => acc.value === item.game_hash)
						if(objs.length<=0){
							accounts.value.push({
								label:item.game_name,
								value:item.game_hash
							})
							objs = accounts.value.filter(acc => acc.value === item.game_hash)
						}
						if(!("children" in objs[0])){
							objs[0].children = []
						}
						objs[0].children.push(itemObj)
					})
				}
			})
			
		})
		console.log(accounts.value)
	}
	getAccounts()
	
	const projects = ref([])
	const getProjects = () => {
		if(projects.value.length>0) return
		rpcGet({server:"HugeServiceServer.GetProjects"}).then(r => {  
			if(r.data.total>0){
					r.data.list.forEach(item=>{
						let value = item.plat_form+","+item.account_id+","+item.project_id
						let itemObj = {
							label:item.project_name,
							value:value
						}
						let objs = projects.value.filter(pro => pro.value === value)
						if(objs.length<=0){
							projects.value.push(itemObj)
						}
					})
					
				}
			
		}).finally(()=>{
		})
	}
	const advs = ref([])
	const getAdvs = ()=>{
		advs.value = []
		let platforms = [1]
		Promise.all(platforms.map(platform => rpcGet({platformId:platform,server:"HugeServiceServer.GetAdv"}))).then(results => {  
			results.forEach(r=>{
				r.data.list.forEach(item=>{
					let value = item.platform+","+item.advertiser_id+","+item.advertisement_id
					let itemObj = {
						label:item.advertisement_name,
						value:value
					}
					let objs = advs.value.filter(acc => acc.value === item.advertiser_id)
					if(objs.length<=0){
						if (item.platform == "3"){
							advs.value.push({
								label:item.advertiser_name+"("+item.advertiser_id+")",
								value:item.advertiser_id
							})
						}else{
							advs.value.push({
								label:item.advertiser_name,
								value:item.advertiser_id
							})
						}
						objs = advs.value.filter(acc => acc.value === item.advertiser_id)
					}
					if(!("children" in objs[0])){
						objs[0].children = []
					}
					objs[0].children.push(itemObj)
				})
			})
		})
	}
	const getAdvs2 = ()=>{
		if(advs.value.length>0) return
		rpcGet({server:"HugeServiceServer.GetAdv"}).then(r => {  
			if(r.data.list instanceof Array){
					r.data.list.forEach(item=>{
						let value = item.advertisement_id
						let itemObj = {
							label:item.advertisement_name,
							value:value
						}
						let objs = advs.value.filter(pro => pro.value === value)
						if(objs.length<=0){
							advs.value.push(itemObj)
						}
					})
					
				}
			
		}).finally(()=>{
			
		})
	}
	const page = ref(1)
	const total = ref(0)
	const pageSize = ref(10)
	const searchInfo = ref({
		name:""
	})
	// 分页
	const handleSizeChange = (val) => {
		pageSize.value = val
		getRules()
	}

	const handleCurrentChange = (val) => {
		page.value = val
		getRules()
	}
	const rulesList = ref([])
	const getRules = () => {
		rulesList.value = []
		rpcGet({server:"HugeServiceServer.ruleList",page: page.value, page_size: pageSize.value,...searchInfo}).then(r => {
			total.value = r.data.total
			if(total.value > 0){
				r.data.list.forEach(item=>{
					let condition = JSON.parse(item.condition)
					let conditionInfo = ""
					condition.forEach(iitem=>{
						let conditionString = ""
						let first = conditionTargetCommon.find(obj => obj.value === iitem.target[0])
						conditionString = first['label']
						if(first && first.children && first.children.length && first.children.length>0){
							let second = first.children.find(obj => obj.value === iitem.target[1])
							conditionString  += "/"+second['label']
						}
						conditionInfo += (conditionInfo !="" ? ";":"")+conditionTime[iitem.time] +""+ conditionString;
					})
					let execute = JSON.parse(item.execute)
					let executeString = ""
					let first = executes[item.objectTo].find(obj => obj.value === execute.opt[0])
					executeString = first['label']
					if(execute.opt[1] && first && first.children && first.children.length && first.children.length>0){
						let second = first.children.find(obj => obj.value === execute.opt[1])
						executeString += ','+second['label']
					}
					rulesList.value.push({
						name:item.name,
						objectTo:objects[item.objectTo],
						conditionInfo:conditionInfo,
						executeInfo:executeString,
						status:item.status === 1,
						detail:item
					})
				})
			}
		})  
	}
	getRules()
	const openEdit = (row)=>{
		drawerForm.value = !drawerForm.value
		let objectParam = JSON.parse(row.detail.objectParam)
		ruleForm.value.name = row.name
		ruleForm.value.objectTo = row.detail.objectTo
		ruleForm.value.accounts = JSON.parse(objectParam.accounts)
		ruleForm.value.projects = JSON.parse(objectParam.projects)
		ruleForm.value.advs = JSON.parse(objectParam.advs)
		ruleForm.value.accountSelectStatus = objectParam.accountSelectStatus
		ruleForm.value.projectSelectStatus = objectParam.projectSelectStatus
		ruleForm.value.advSelectStatus = objectParam.advSelectStatus
		ruleForm.value.condition = JSON.parse(row.detail.condition)
		ruleForm.value.execute = JSON.parse(row.detail.execute)
		ruleForm.value.executeTime = JSON.parse(row.detail.executeTime)
		ruleForm.value.id = row.detail.id
	}
	const del = (row)=>{
		rpcGet({server:"HugeServiceServer.ruleDel",id:row.detail.id}).then(r => { 
			if(r.code == 0){
						ElMessage({
							type: 'success',
							message: '操作成功',
						})
						row.visible = false
						getRules()
					}

		 })
	}
	const statusChange = (row)=>{
		rpcGet({server:"HugeServiceServer.ruleStatus",id:row.detail.id}).then(r => { 
			if(r.code == 0){
				ElMessage({
					type: 'success',
					message: '操作成功',
				})
			}

		 })
	}
</script>
<style>
</style>